package com.sctvcloud.qingchuan.ui.utils;

import com.google.gson.Gson;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.beans.PoliticsReplyDotBean;

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Author: create by Administrator on 2019/7/1 22 07
 * Des:
 */
public class DataUtils {
    private static double DEF_PI = 3.14159265359; // PI
    private static double DEF_2PI = 6.28318530712; // 2*PI
    private static double DEF_PI180 = 0.01745329252; // PI/180.0
    private static double DEF_R = 6370693.5; // radius of earth

    /**
     * 数据对象转换成json字符串
     *
     * @param ob
     * @return
     */
    public static String toJson(Object ob) {
        Gson gson = new Gson();
        String bodyStr = gson.toJson(ob);
        return bodyStr;
    }

    /**
     * 将数据对象转换成 RequestBody对象
     *
     * @param ob
     * @return
     */
    public static RequestBody toRequestBody(Object ob) {
        Gson gson = new Gson();
        String bodyStr = gson.toJson(ob);
        JLog.e("--toRequestBody-- " + bodyStr);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), bodyStr);
        return body;
    }

    /**
     * 转换为 form-data
     *
     * @param requestDataMap
     * @return
     */
    public static Map<String, RequestBody> generateRequestBody(Map<String, String> requestDataMap) {
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : requestDataMap.keySet()) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
            requestBodyMap.put(key, requestBody);
        }
        return requestBodyMap;
    }

    /**
     * 转换为 form-data
     *
     * @param requestDataMap
     * @return
     */
    public static Map<String, RequestBody> generateRequestBody1(Map<String, Object> requestDataMap) {
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : requestDataMap.keySet()) {
            if (requestDataMap.get(key) instanceof String) {
                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), (String) requestDataMap.get(key) == null ? "" : (String) requestDataMap.get(key));
                requestBodyMap.put(key, requestBody);
            } else if (requestDataMap.get(key) instanceof File) {
                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), (File) requestDataMap.get(key));
                requestBodyMap.put(key, requestBody);
            }
        }
        return requestBodyMap;
    }


    /**
     * 通过上传的文件的完整路径生成RequestBody
     *
     * @param fileNames 完整的文件路径
     * @return
     */
    public static RequestBody getRequestBody(List<String> fileNames) {
        //创建MultipartBody.Builder，用于添加请求的数据
        MultipartBody.Builder builder = new MultipartBody.Builder();

        builder.addFormDataPart( //给Builder添加上传的文件
                "tenantId", Constances.TENANT_ID); //创建RequestBody，把上传的文件放入
        builder.addFormDataPart( //给Builder添加上传的文件
                "tenantId", Constances.TENANT_ID); //创建RequestBody，把上传的文件放入
        for (int i = 0; i < fileNames.size(); i++) { //对文件进行遍历
            File file = new File(fileNames.get(i)); //生成文件
            //根据文件的后缀名，获得文件类型
            String fileType = getMimeType(file.getName());
            builder.addFormDataPart( //给Builder添加上传的文件
                    "image",  //请求的名字
                    file.getName(), //文件的文字，服务器端用来解析的
                    RequestBody.create(MediaType.parse(fileType), file) //创建RequestBody，把上传的文件放入
            );
        }
        return builder.build(); //根据Builder创建请求
    }

    public static String getMimeType(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            String type = fileNameMap.getContentTypeFor(file.getName());
            return type;
        } else {
            return "";
        }

    }

    /**
     * 计算折扣
     *
     * @param price
     * @param oldPrice
     * @return
     */
    public static String calDis(double price, double oldPrice) {
        if (price == 0 || oldPrice == 0) {
            return "0";
        } else {
            double d = price / oldPrice;
            int intD = (int) (d * 100);
            float fd = intD / 10;
            return "" + fd;
        }

    }

    /**
     * 返回为m，适合短距离测量
     * 百度地图
     *
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     * @return
     */
    public static String getShortDistance(double lon1, double lat1, double lon2, double lat2) {
        double ew1, ns1, ew2, ns2;
        double dx, dy, dew;
        double distance;
        // 角度转换为弧度
        ew1 = lon1 * DEF_PI180;
        ns1 = lat1 * DEF_PI180;
        ew2 = lon2 * DEF_PI180;
        ns2 = lat2 * DEF_PI180;
        // 经度差
        dew = ew1 - ew2;
        // 若跨东经和西经180 度，进行调整
        if (dew > DEF_PI)
            dew = DEF_2PI - dew;
        else if (dew < -DEF_PI)
            dew = DEF_2PI + dew;
        dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
        dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
        // 勾股定理求斜边长
        distance = Math.sqrt(dx * dx + dy * dy);
        return trans(distance);
    }

    /**
     * 返回为m,适合长距离测量
     * * 百度地图
     *
     * @param lon1
     * @param lat1
     * @param lon2
     * @param lat2
     * @return
     */
    public static String getLongDistance(double lon1, double lat1, double lon2, double lat2) {
        double ew1, ns1, ew2, ns2;
        double distance;
        // 角度转换为弧度
        ew1 = lon1 * DEF_PI180;
        ns1 = lat1 * DEF_PI180;
        ew2 = lon2 * DEF_PI180;
        ns2 = lat2 * DEF_PI180;
        // 求大圆劣弧与球心所夹的角(弧度)
        distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2)
                * Math.cos(ew1 - ew2);
        // 调整到[-1..1]范围内，避免溢出
        if (distance > 1.0)
            distance = 1.0;
        else if (distance < -1.0)
            distance = -1.0;
        // 求大圆劣弧长度
        distance = DEF_R * Math.acos(distance);
        return trans(distance);
    }

    private static String trans(double distance) {
        boolean isBig = false; // 是否为大于等于1000m
        if (distance >= 1000) {
            distance /= 1000;
            isBig = true;
        }
        return (new DecimalFormat(".00").format(distance)) + (isBig ? "千米" : "米");
    }

    /**
     * 普通字符转换成16进制字符串
     *
     * @param pr
     * @return
     */
    public static String str2HexStr(int pr) {
        String pf = "00";
        if (pr >= 90) {
            pf = "F2";
        } else if (pr >= 80 && pr < 90) {
            pf = "E6";
        } else if (pr >= 70 && pr < 80) {
            pf = "CC";
        } else if (pr >= 60 && pr < 70) {
            pf = "B3";
        } else if (pr >= 50 && pr < 60) {
            pf = "99";
        } else if (pr >= 40 && pr < 50) {
            pf = "80";
        } else if (pr >= 30 && pr < 40) {
            pf = "66";
        } else if (pr >= 20 && pr < 30) {
            pf = "4D";
        } else if (pr >= 10 && pr < 20) {
            pf = "33";
        } else if (pr < 10) {
            pf = "11";
        }
        return pf;
    }

    /**
     * 遍历 将某各融媒号的回复数据整合成一级列表
     * @param replyList
     * @return
     */
    public static List<PoliticsReplyDotBean> handlePoliticsReply(List<PoliticsReplyDotBean> replyList) {
        if (ListUtils.isListValued(replyList)) {
            for (int i = 0; i < replyList.size(); i++) {
                if (ListUtils.isListValued(replyList.get(i).getInteractionReplyDtoList())) {
                    List<PoliticsReplyDotBean> list1 = handlePoliticsReply(replyList.get(i).getInteractionReplyDtoList());
                    replyList.get(i).setInteractionReplyDtoList(null);
                    if(i<replyList.size()-1){
                        replyList.addAll(i+1,list1);
                    }else {
                        replyList.addAll(list1);
                    }
                }
            }
            return replyList;
        } else {
            return null;
        }
    }

}
